package com.cloudoer.blog.service.impl;

import com.cloudoer.blog.base.BaseService;
import com.cloudoer.blog.model.Album;
import com.cloudoer.blog.model.info.AlbumInfo;
import com.cloudoer.blog.service.AlbumService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service(value = "albumService")
public class AlbumServiceImpl extends BaseService implements AlbumService {
    @Override
    public Album getById(String id) {
        return albumDao.selectByPrimaryKey(id);
    }

    @Override
    public List<Album> getAll() {
        return albumDao.selectAll();
    }

    @Override
    public List<Album> getByPage(int pstart, int psize) {
        return albumDao.selectByPage(pstart, psize);
    }

    @Override
    public List<Album> getByRanking() {
        return albumDao.selectByRanking();
    }

    @Override
    public List<AlbumInfo> getByPageInfo(int pstart, int psize) {
        return albumDao.selectByPageInfo(pstart, psize);
    }

    @Override
    public List<AlbumInfo> getByRankingInfo() {
        return albumDao.selectByRankingInfo();
    }

    @Override
    public String add(Album album) {
        album.setId(UUID.randomUUID().toString().replace("-", ""));
        album.setPostDate(new Date());
        albumDao.insertSelective(album);
        return album.getId();
    }

    @Override
    public int update(Album album) {
        return albumDao.updateByPrimaryKeySelective(album);
    }

    @Override
    public int delete(String id) {
        return albumDao.deleteByPrimaryKey(id);
    }

    @Override
    public int getCount() {
        return albumDao.selectCount();
    }

    @Override
    public void endorse(String id) {
        Album album = albumDao.selectByPrimaryKey(id);
        album.setEndorse(album.getEndorse()+1);
        albumDao.updateByPrimaryKeySelective(album);
    }
}
